home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / hity wydania / Ubuntu 9.10 PL / karmelkowy-koliberek-desktop-9.10-i386-PL.iso / casper / filesystem.squashfs / usr / lib / python2.6 / Queue.pyc (.txt) < prev    next >
Python Compiled Bytecode  |  2009-11-11  |  9KB  |  277 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. '''A multi-producer, multi-consumer queue.'''
  5. from time import time as _time
  6. from collections import deque
  7. import heapq
  8. __all__ = [
  9.     'Empty',
  10.     'Full',
  11.     'Queue',
  12.     'PriorityQueue',
  13.     'LifoQueue']
  14.  
  15. class Empty(Exception):
  16.     '''Exception raised by Queue.get(block=0)/get_nowait().'''
  17.     pass
  18.  
  19.  
  20. class Full(Exception):
  21.     '''Exception raised by Queue.put(block=0)/put_nowait().'''
  22.     pass
  23.  
  24.  
  25. class Queue:
  26.     '''Create a queue object with a given maximum size.
  27.  
  28.     If maxsize is <= 0, the queue size is infinite.
  29.     '''
  30.     
  31.     def __init__(self, maxsize = 0):
  32.         
  33.         try:
  34.             import threading as threading
  35.         except ImportError:
  36.             import dummy_threading as threading
  37.  
  38.         self.maxsize = maxsize
  39.         self._init(maxsize)
  40.         self.mutex = threading.Lock()
  41.         self.not_empty = threading.Condition(self.mutex)
  42.         self.not_full = threading.Condition(self.mutex)
  43.         self.all_tasks_done = threading.Condition(self.mutex)
  44.         self.unfinished_tasks = 0
  45.  
  46.     
  47.     def task_done(self):
  48.         '''Indicate that a formerly enqueued task is complete.
  49.  
  50.         Used by Queue consumer threads.  For each get() used to fetch a task,
  51.         a subsequent call to task_done() tells the queue that the processing
  52.         on the task is complete.
  53.  
  54.         If a join() is currently blocking, it will resume when all items
  55.         have been processed (meaning that a task_done() call was received
  56.         for every item that had been put() into the queue).
  57.  
  58.         Raises a ValueError if called more times than there were items
  59.         placed in the queue.
  60.         '''
  61.         self.all_tasks_done.acquire()
  62.         
  63.         try:
  64.             unfinished = self.unfinished_tasks - 1
  65.             if unfinished <= 0:
  66.                 if unfinished < 0:
  67.                     raise ValueError('task_done() called too many times')
  68.                 unfinished < 0
  69.                 self.all_tasks_done.notify_all()
  70.             
  71.             self.unfinished_tasks = unfinished
  72.         finally:
  73.             self.all_tasks_done.release()
  74.  
  75.  
  76.     
  77.     def join(self):
  78.         '''Blocks until all items in the Queue have been gotten and processed.
  79.  
  80.         The count of unfinished tasks goes up whenever an item is added to the
  81.         queue. The count goes down whenever a consumer thread calls task_done()
  82.         to indicate the item was retrieved and all work on it is complete.
  83.  
  84.         When the count of unfinished tasks drops to zero, join() unblocks.
  85.         '''
  86.         self.all_tasks_done.acquire()
  87.         
  88.         try:
  89.             while self.unfinished_tasks:
  90.                 self.all_tasks_done.wait()
  91.         finally:
  92.             self.all_tasks_done.release()
  93.  
  94.  
  95.     
  96.     def qsize(self):
  97.         '''Return the approximate size of the queue (not reliable!).'''
  98.         self.mutex.acquire()
  99.         n = self._qsize()
  100.         self.mutex.release()
  101.         return n
  102.  
  103.     
  104.     def empty(self):
  105.         '''Return True if the queue is empty, False otherwise (not reliable!).'''
  106.         self.mutex.acquire()
  107.         n = not self._qsize()
  108.         self.mutex.release()
  109.         return n
  110.  
  111.     
  112.     def full(self):
  113.         '''Return True if the queue is full, False otherwise (not reliable!).'''
  114.         self.mutex.acquire()
  115.         n = None if self.maxsize < self.maxsize else self.maxsize == self._qsize()
  116.         self.mutex.release()
  117.         return n
  118.  
  119.     
  120.     def put(self, item, block = True, timeout = None):
  121.         """Put an item into the queue.
  122.  
  123.         If optional args 'block' is true and 'timeout' is None (the default),
  124.         block if necessary until a free slot is available. If 'timeout' is
  125.         a positive number, it blocks at most 'timeout' seconds and raises
  126.         the Full exception if no free slot was available within that time.
  127.         Otherwise ('block' is false), put an item on the queue if a free slot
  128.         is immediately available, else raise the Full exception ('timeout'
  129.         is ignored in that case).
  130.         """
  131.         self.not_full.acquire()
  132.         
  133.         try:
  134.             if self.maxsize > 0:
  135.                 if not block:
  136.                     if self._qsize() == self.maxsize:
  137.                         raise Full
  138.                     self._qsize() == self.maxsize
  139.                 elif timeout is None:
  140.                     while self._qsize() == self.maxsize:
  141.                         self.not_full.wait()
  142.                 elif timeout < 0:
  143.                     raise ValueError("'timeout' must be a positive number")
  144.                 else:
  145.                     endtime = _time() + timeout
  146.                     while self._qsize() == self.maxsize:
  147.                         remaining = endtime - _time()
  148.                         if remaining <= 0:
  149.                             raise Full
  150.                         remaining <= 0
  151.                         self.not_full.wait(remaining)
  152.             
  153.             self._put(item)
  154.             self.unfinished_tasks += 1
  155.             self.not_empty.notify()
  156.         finally:
  157.             self.not_full.release()
  158.  
  159.  
  160.     
  161.     def put_nowait(self, item):
  162.         '''Put an item into the queue without blocking.
  163.  
  164.         Only enqueue the item if a free slot is immediately available.
  165.         Otherwise raise the Full exception.
  166.         '''
  167.         return self.put(item, False)
  168.  
  169.     
  170.     def get(self, block = True, timeout = None):
  171.         """Remove and return an item from the queue.
  172.  
  173.         If optional args 'block' is true and 'timeout' is None (the default),
  174.         block if necessary until an item is available. If 'timeout' is
  175.         a positive number, it blocks at most 'timeout' seconds and raises
  176.         the Empty exception if no item was available within that time.
  177.         Otherwise ('block' is false), return an item if one is immediately
  178.         available, else raise the Empty exception ('timeout' is ignored
  179.         in that case).
  180.         """
  181.         self.not_empty.acquire()
  182.         
  183.         try:
  184.             if not block:
  185.                 if not self._qsize():
  186.                     raise Empty
  187.                 self._qsize()
  188.             elif timeout is None:
  189.                 while not self._qsize():
  190.                     self.not_empty.wait()
  191.             elif timeout < 0:
  192.                 raise ValueError("'timeout' must be a positive number")
  193.             else:
  194.                 endtime = _time() + timeout
  195.                 while not self._qsize():
  196.                     remaining = endtime - _time()
  197.                     if remaining <= 0:
  198.                         raise Empty
  199.                     remaining <= 0
  200.                     self.not_empty.wait(remaining)
  201.             item = self._get()
  202.             self.not_full.notify()
  203.             return item
  204.         finally:
  205.             self.not_empty.release()
  206.  
  207.  
  208.     
  209.     def get_nowait(self):
  210.         '''Remove and return an item from the queue without blocking.
  211.  
  212.         Only get an item if one is immediately available. Otherwise
  213.         raise the Empty exception.
  214.         '''
  215.         return self.get(False)
  216.  
  217.     
  218.     def _init(self, maxsize):
  219.         self.queue = deque()
  220.  
  221.     
  222.     def _qsize(self, len = len):
  223.         return len(self.queue)
  224.  
  225.     
  226.     def _put(self, item):
  227.         self.queue.append(item)
  228.  
  229.     
  230.     def _get(self):
  231.         return self.queue.popleft()
  232.  
  233.  
  234.  
  235. class PriorityQueue(Queue):
  236.     '''Variant of Queue that retrieves open entries in priority order (lowest first).
  237.  
  238.     Entries are typically tuples of the form:  (priority number, data).
  239.     '''
  240.     
  241.     def _init(self, maxsize):
  242.         self.queue = []
  243.  
  244.     
  245.     def _qsize(self, len = len):
  246.         return len(self.queue)
  247.  
  248.     
  249.     def _put(self, item, heappush = heapq.heappush):
  250.         heappush(self.queue, item)
  251.  
  252.     
  253.     def _get(self, heappop = heapq.heappop):
  254.         return heappop(self.queue)
  255.  
  256.  
  257.  
  258. class LifoQueue(Queue):
  259.     '''Variant of Queue that retrieves most recently added entries first.'''
  260.     
  261.     def _init(self, maxsize):
  262.         self.queue = []
  263.  
  264.     
  265.     def _qsize(self, len = len):
  266.         return len(self.queue)
  267.  
  268.     
  269.     def _put(self, item):
  270.         self.queue.append(item)
  271.  
  272.     
  273.     def _get(self):
  274.         return self.queue.pop()
  275.  
  276.  
  277.